<html>
<head><meta charset="utf-8"><title>TraitRef to the DefId for its impl · t-compiler/help · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/index.html">t-compiler/help</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/TraitRef.20to.20the.20DefId.20for.20its.20impl.html">TraitRef to the DefId for its impl</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="218241788"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/TraitRef%20to%20the%20DefId%20for%20its%20impl/near/218241788" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> James <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/TraitRef.20to.20the.20DefId.20for.20its.20impl.html#218241788">(Nov 30 2020 at 01:55)</a>:</h4>
<p>Hey there! I'm working (at least trying to work on) <a href="https://github.com/rust-lang/rust/issues/79458">#79458</a>. For this, I believe that I need to get a <code>DefId</code> for the <code>impl</code> corresponding to a <code>TraitRef</code>. There's already the <code>TyCtxt</code> <code>impl_trait_ref</code> query for going from a <code>DefId</code> of an <code>impl</code> to a <code>TraitRef</code>, but not the other way around. <code>impl_polarity</code> takes a <code>DefId</code> of an <code>impl</code>, and I'm provided with a <code>TraitRef</code>. Is there a way to do this? (I tried filtering out negative polarity <code>impl</code>s in <a href="https://github.com/rust-lang/rust/blob/master/compiler/rustc_trait_selection/src/traits/error_reporting/mod.rs#L1296-L1318">this code</a> but it caused a bunch of test failures, which should not have happened).<br>
P.S. sorry if this is kind of disorganized of a question, I'm new to contributing and really confused why my changes didn't work correctly.</p>



<a name="218241846"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/TraitRef%20to%20the%20DefId%20for%20its%20impl/near/218241846" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/TraitRef.20to.20the.20DefId.20for.20its.20impl.html#218241846">(Nov 30 2020 at 01:57)</a>:</h4>
<p>TraitRef has a def_id field, is that what you're looking for? <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.TraitRef.html#structfield.def_id">https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.TraitRef.html#structfield.def_id</a></p>



<a name="218241863"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/TraitRef%20to%20the%20DefId%20for%20its%20impl/near/218241863" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> James <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/TraitRef.20to.20the.20DefId.20for.20its.20impl.html#218241863">(Nov 30 2020 at 01:58)</a>:</h4>
<p>the <code>def_id</code> on TraitRef is for the trait (i.e. <code>trait Trait</code>), not the impl (<code>impl Trait for Ty</code>)</p>



<a name="218241890"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/TraitRef%20to%20the%20DefId%20for%20its%20impl/near/218241890" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/TraitRef.20to.20the.20DefId.20for.20its.20impl.html#218241890">(Nov 30 2020 at 01:58)</a>:</h4>
<blockquote>
<p>I tried filtering out negative polarity impls in this code but it caused a bunch of test failures, which should not have happened</p>
</blockquote>
<p>FYI, any change in diagnostics at all will cause tests to fail, so it might not have been the wrong change</p>



<a name="218241894"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/TraitRef%20to%20the%20DefId%20for%20its%20impl/near/218241894" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> James <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/TraitRef.20to.20the.20DefId.20for.20its.20impl.html#218241894">(Nov 30 2020 at 01:58)</a>:</h4>
<p>It was incorrectly filtering out trait suggestions that should've been there afaik but I'll check again I guess</p>



<a name="218241895"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/TraitRef%20to%20the%20DefId%20for%20its%20impl/near/218241895" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/TraitRef.20to.20the.20DefId.20for.20its.20impl.html#218241895">(Nov 30 2020 at 01:58)</a>:</h4>
<p>oh I see - you know there can only be one implementation because of coherence, and you want to find it</p>



<a name="218241899"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/TraitRef%20to%20the%20DefId%20for%20its%20impl/near/218241899" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/TraitRef.20to.20the.20DefId.20for.20its.20impl.html#218241899">(Nov 30 2020 at 01:59)</a>:</h4>
<p>ah ok, you'll know better than me whether the change was correct :)</p>



<a name="218241954"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/TraitRef%20to%20the%20DefId%20for%20its%20impl/near/218241954" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Joshua Nelson <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/TraitRef.20to.20the.20DefId.20for.20its.20impl.html#218241954">(Nov 30 2020 at 02:00)</a>:</h4>
<p>this looks vaguely related, although you can't filter by the implementing type: <a href="https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.TyCtxt.html#method.implementations_of_trait">https://doc.rust-lang.org/nightly/nightly-rustc/rustc_middle/ty/struct.TyCtxt.html#method.implementations_of_trait</a></p>



<a name="218242025"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/TraitRef%20to%20the%20DefId%20for%20its%20impl/near/218242025" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> James <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/TraitRef.20to.20the.20DefId.20for.20its.20impl.html#218242025">(Nov 30 2020 at 02:03)</a>:</h4>
<p>There's also <code>all_impls</code> (or whatever it's called) which is used in <code>impl_trait_ref</code> but then filtering on that for every suggested implementation would end up being at least O(n^2) so I'd rather not do that.</p>



<a name="218459705"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/182449-t-compiler/help/topic/TraitRef%20to%20the%20DefId%20for%20its%20impl/near/218459705" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> James <a href="https://rust-lang.github.io/zulip_archive/stream/182449-t-compiler/help/topic/TraitRef.20to.20the.20DefId.20for.20its.20impl.html#218459705">(Dec 01 2020 at 18:08)</a>:</h4>
<p>Turns out some of those test failures actually were correct, so I'm going to re-run tests again with my original changes then see which ones are valid (hopefully, all of them). After that, I'll add a regression test.</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>